package ihm;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

import Utilities.GraphicsConstant;

public class WindowRes extends JPanel implements GraphicsConstant {
	
	private JButton options;
	private JButton database;
	private JLabel label;
	private JPanel imgPanel;
	private List<JComponent> compList;
	
	private CardLayout cards;
	private JPanel cardPanel;
	
	
	public WindowRes(CardLayout cards, final JPanel cardPanel) {	
		
		if(label==null) {
			label = new JLabel(); 
		}

		compList = new ArrayList<JComponent>();		
		imgPanel = new JPanel();
		
		this.cards=cards;
		this.cardPanel=cardPanel;
		
	
		
		this.buildSkeleton();
		this.buildCSS();
		
		
		options.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				System.err.println("switch from RES to OptionWindow");
//				cards.show(cardPanel,"ResWindow");
				((CardLayout) cardPanel.getLayout()).show(cardPanel,"OptionWindow");
			}
			
		});
		
		
		
		database.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
//				cards.show(cardPanel,"OptionWindow");
				System.err.println("switch from res to DBwINDOW");
				((CardLayout) cardPanel.getLayout()).show(cardPanel,"DBWindow");
				repaint();
			}
			
		});
	}

	

	
	private void buildSkeleton() {

		label.setSize(1000,750);
		this.setLayout(new BorderLayout());
		this.add(label, BorderLayout.CENTER);
		
		options = new JButton("Fenetre Options");
		database = new JButton("Fenetre DB");
		
		options.setVisible(true);
		database.setVisible(true);
		
		compList.add(options);
		compList.add(database);
	}

	private void buildCSS() {

		//build background	
		ImageIcon icon = new ImageIcon(Toolkit.getDefaultToolkit().getImage("src/images/resultat.png")); 
		JLabel img = new JLabel(icon);
		imgPanel.add(img);
		imgPanel.setBounds(0,-5,1200,1500);
		
		
		
		options.setBounds(100,100,smallBtnWidth,smallBtnHeight);
		database.setBounds(100,250,smallBtnWidth,smallBtnHeight);
		
		LineBorder lb = new LineBorder(Color.GRAY, 2);
		options.setBorder(lb);
		database.setBorder(lb);
		
		
		for(JComponent comp : compList) {
			label.add(comp);
		}
		
		
		// on l'ajoute a la fin sinon le fond est dessiner par dessus les composants et on ne voit QUE lui :) 
		label.add(imgPanel);
	}

	
	/**
	 * 
	 * Getters & Setters
	 * 
	 */
	public JLabel getMasterLabel () {
		return this.label;
	}
	
	public void setMasterLabel(JLabel label) {
		this.label=label;
	}
	
	
	
}
